<script>
export default {
  name: "UpdatePwdDialog",

    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        dialogVisible: false,
        ruleForm: {
          id:'',
          pwd: '',
          checkPass: '',
        },
        rules: {
          pwd: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      //显示对话框
      show_dialog(){
          this.dialogVisible = true;
      },

      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const _this = this;
            const acc_id = sessionStorage.getItem("acc_id");
            this.ruleForm.id = acc_id;
            //提交表单数据，修改密码
            this.$http.post("accounts/change_pwd",this.ruleForm).then(res=>{
                if(res.data.success){
                  _this.$message.success("修改成功！");
                  _this.dialogVisible = false;
                  _this.$router.push("/login");
                }else {
                  _this.$message.error(res.data.errMsg);
                }
            })

          } else {

            return false;
          }
        });
      },

      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.dialogVisible = false;
      }

    }

}
</script>

<template>
  <el-dialog
      title="修改密码"
      :visible.sync="dialogVisible"
      width="30%"

  >

    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="密码" prop="pwd">
        <el-input type="password" v-model="ruleForm.pwd" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">提 交</el-button>
     </span>
  </el-dialog>
</template>

<style scoped>

</style>